//
//  Colors.swift
//  AICleaner
//
//  Created by firefly on 2025/1/25.
//

import SwiftUI

extension Color {
    
    init?(hex: String) {
        var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines)
        hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "")

        var rgb: UInt64 = 0

        // 将 16 进制字符串转换为数字
        Scanner(string: hexSanitized).scanHexInt64(&rgb)

        // 检查字符串长度
        let red, green, blue: CGFloat
        switch hexSanitized.count {
        case 3: // RGB (12-bit)
            red = CGFloat((rgb & 0xF00) >> 8) / 15.0
            green = CGFloat((rgb & 0x0F0) >> 4) / 15.0
            blue = CGFloat(rgb & 0x00F) / 15.0
        case 6: // RGB (24-bit)
            red = CGFloat((rgb & 0xFF0000) >> 16) / 255.0
            green = CGFloat((rgb & 0x00FF00) >> 8) / 255.0
            blue = CGFloat(rgb & 0x0000FF) / 255.0
        default:
            return nil
        }

        self.init(red: red, green: green, blue: blue)
    }
    
    static let primaryColor = Color(hex: "0A0E2F")
}
